#include "getCode.h"
#include "imageSDL.h"
#include "math.h"

SDL_Surface getCodeSurface(SDL_Surface* source)
{
    /** Checks the base image for the zone witch contains the code**/
    randInit();
    int discovered = 0;

    while(!discovered)
    {
        int x = getRand(source->w), y = getRand(source->h);
        //discovered = scanAround(source, x, y);
    }
    /* PLACEHOLDER */
    return ;
}

Code1D getCode1D(SDL_Surface* codeSurface)
{
    SDL_PixelFormat* fmt = codeSurface->format;
    Code1D code;
    int i;

    /*size is stored in the width of the image*/
    code.size = codeSurface->w;
    int charSize = code1D_charSize(&code);
    code.array = malloc((charSize+1)*sizeof(char));
    /*allocation of the proper number of chars*/

    SDL_LockSurface(codeSurface);
    for (i=0;i<charSize-1;i++)
    {
        /* we increment the pointer by 24 = 8 pixels * 3 component per pixel (RGB)*/
        code.array[i] = charFromPixels(((Uint8*)codeSurface->pixels)+24*i, 8, fmt);
    }
    /* special treatment for the last char given that the image may not be n*8 in size*/
    code.array[i] = charFromPixels(((Uint8*)codeSurface->pixels)+24*i, code.size-8*i, fmt);
    code.array[i+1] = '\0'; /*NULL terminated*/

    SDL_UnlockSurface(codeSurface);

    return code;
}

void freeCode1D(Code1D* code)
{
    free(code->array);
}

